---
title: Switch to Turso
description: How to change the database provider to Turso.
---

[Turso](https://turso.tech) is multi-tenant database platform built for all types of apps, including AI apps with on-device RAG, local-first vector search, offline writes, and privacy-focused data access with low latency.

Here's how to switch from Neon to [Turso](https://turso.tech) for your `next-forge` project.

## 1. Sign up to Turso

You can use the [Dashboard](https://app.turso.tech), or the [CLI](https://docs.turso.tech/cli) to manage your account, database, and auth tokens.

_We'll be using the CLI throughout this guide._

## 2. Create a Database

Create a new database and give it a name using the Turso CLI:

```sh title="Terminal"
turso db create <database-name>
```

You can now fetch the URL to the database:

```sh title="Terminal"
turso db show <database-name> --url
```

It will look something like this:

```
libsql://<database-name>-<account-or-org-slug>.turso.io
```

## 3. Create a Database Auth Token

You will need to create an auth token to connect to your Turso database:

```sh title="Terminal"
turso db tokens create <database-name>
```

## 4. Update your environment variables

Update your environment variables to use the new Turso connection string:

```js title="apps/database/.env"
DATABASE_URL="libsql://<database-name>-<account-or-org-slug>.turso.io"
DATABASE_AUTH_TOKEN="..."
```

```js title="apps/app/.env.local"
DATABASE_URL="libsql://<database-name>-<account-or-org-slug>.turso.io"
DATABASE_AUTH_TOKEN="..."
```

Etcetera.

Now inside `packages/env/index.ts`, add `DATABASE_AUTH_TOKEN` to the `server` and `runtimeEnv` objects:

```ts title="{3,12}"
const server: Parameters<typeof createEnv>[0]["server"] = {
  // ...
  DATABASE_AUTH_TOKEN: z.string(),
  // ...
};

export const env = createEnv({
  client,
  server,
  runtimeEnv: {
    // ...
    DATABASE_AUTH_TOKEN: process.env.DATABASE_AUTH_TOKEN,
    // ...
  },
});
```

## 5. Install @libsql/client

The [`@libsql/client`](https://www.npmjs.com/@libsql/client) is used to connect to the hosted Turso database.

Uninstall the existing dependencies for Neon...

```sh title="Terminal"
pnpm remove @neondatabase/serverless @prisma/adapter-neon ws @types/ws --filter @repo/database
```

... and install the new dependencies for Turso & libSQL:

```sh title="Terminal"
pnpm add @libsql/client --filter @repo/database
```

## 6. Update the database connection code

Open `packages/database/index.ts` and make the following changes:

```ts title="packages/database/index.ts"
import "server-only";

import { createClient } from "@libsql/client";
import { env } from "@repo/env";

const libsql = createClient({
  url: env.DATABASE_URL,
  authToken: env.DATABASE_AUTH_TOKEN,
});

export const database = libsql;
```

## 7. Apply schema changes

Now connect to the Turso database using the CLI:

```sh title="Terminal"
turso db shell <database-name>
```

And apply the schema to the database:

```sql
CREATE TABLE pages (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  email TEXT UNIQUE NOT NULL,
  name TEXT
);
```

## 8. Update application code

Now wherever you would usually call Prisma, use the `libsql` client instead:

```ts title="packages/app/app/(authenticated)/page.tsx"
import { database } from "@repo/database";

type PageType = {
  id: number;
  email: string;
  name?: string;
};

// ...

const { rows } = await database.execute(`SELECT * FROM pages`);

const pages = rows as unknown as Array<PageType>;
```

